package com.ojoin.trade.supplyloan_core.utils.catchdata.httpclient;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ojoin.trade.common.utils.LogUtils;

public class HttpsClientUtil {

	private Logger log = LoggerFactory.getLogger(HttpsClientUtil.class);

	public HttpResponse loginByPos(HttpClient httpClient, String url,
			Map<String, String> map, String charset) {
		// HttpClient httpClient = null;
		HttpPost httpPost = null;
		HttpResponse response = null;
		try {
			// httpClient = new SSLClient();
			httpPost = new HttpPost(url);
			// 设置参数
			List<NameValuePair> list = new ArrayList<NameValuePair>();
			Iterator<Entry<String, String>> iterator = map.entrySet()
					.iterator();
			while (iterator.hasNext()) {
				Entry<String, String> elem = (Entry<String, String>) iterator
						.next();
				list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
			}
			if (list.size() > 0) {
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,
						charset);
				httpPost.setEntity(entity);
			}

			response = httpClient.execute(httpPost);

			if (null != response) {
				int statuscode = response.getStatusLine().getStatusCode();
				if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
						|| (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
						|| (statuscode == HttpStatus.SC_SEE_OTHER)
						|| (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {// 读取新的URL地址

					
					Header[] heads = response.getHeaders("location");

					String locationURL = heads[0].toString();

					locationURL = locationURL.substring(10);

					loginByGet(httpClient, locationURL, charset);

					// Header header = httpPost.getResponseHeader("location");
				}

			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return response;
	}

	public HttpResponse loginByGet(HttpClient httpClient, String url,
			String charset) {
		// HttpClient httpClient = null;
		HttpGet httpGet = null;
		HttpResponse response = null;
		try {
			// httpClient = new SSLClient();
			httpGet = new HttpGet(url);
			// 设置参数

			response = httpClient.execute(httpGet);

			if (null != response) {
				int statuscode = response.getStatusLine().getStatusCode();
				if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
						|| (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
						|| (statuscode == HttpStatus.SC_SEE_OTHER)
						|| (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {// 读取新的URL地址

					Header[] heads = response.getHeaders("location");

					String locationURL = heads[0].toString();

					locationURL = locationURL.substring(10);

					loginByGet(httpClient, locationURL, charset);

					// Header header = httpPost.getResponseHeader("location");
				}

			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return response;
	}

	public HttpResponse doPostReturnResponse(HttpClient httpClient, String url,
			Map<String, String> map, String charset) {
		// HttpClient httpClient = null;
		HttpPost httpPost = null;
		HttpResponse response = null;
		try {
			// httpClient = new SSLClient();
			httpPost = new HttpPost(url);
			// 设置参数
			List<NameValuePair> list = new ArrayList<NameValuePair>();
			Iterator<Entry<String, String>> iterator = map.entrySet()
					.iterator();
			while (iterator.hasNext()) {
				Entry<String, String> elem = (Entry<String, String>) iterator
						.next();
				list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
			}
			if (list.size() > 0) {
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,
						charset);
				httpPost.setEntity(entity);
			}

			response = httpClient.execute(httpPost);

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return response;
	}

	public String doPost(HttpClient httpClient, String url,
			Map<String, String> map, String charset) {
		// HttpClient httpClient = null;
		HttpPost httpPost = null;
		String result = null;
		try {
			// httpClient = new SSLClient();
			httpPost = new HttpPost(url);
			// 设置参数
			List<NameValuePair> list = new ArrayList<NameValuePair>();
			Iterator<Entry<String, String>> iterator = map.entrySet()
					.iterator();
			while (iterator.hasNext()) {
				Entry<String, String> elem = (Entry<String, String>) iterator
						.next();
				list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
			}
			if (list.size() > 0) {
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,
						charset);
				httpPost.setEntity(entity);
			}

			HttpResponse response = httpClient.execute(httpPost);
			if (response != null) {
				StatusLine statuesLine = response.getStatusLine();
				LogUtils.info(log, statuesLine.toString());

				HttpEntity resEntity = response.getEntity();
				if (resEntity != null) {
					result = EntityUtils.toString(resEntity, charset);
				}
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}

	public String doPost(String url, Map<String, String> map, String charset) {
		HttpClient httpClient = null;
		HttpPost httpPost = null;
		String result = null;
		try {
			httpClient = new SSLClient();
			httpPost = new HttpPost(url);
			// 设置参数
			List<NameValuePair> list = new ArrayList<NameValuePair>();
			Iterator<Entry<String, String>> iterator = map.entrySet()
					.iterator();
			while (iterator.hasNext()) {
				Entry<String, String> elem = (Entry<String, String>) iterator
						.next();
				list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
			}
			if (list.size() > 0) {
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,
						charset);
				httpPost.setEntity(entity);
			}

			HttpResponse response = httpClient.execute(httpPost);
			if (response != null) {
				StatusLine statuesLine = response.getStatusLine();
				LogUtils.info(log, statuesLine.toString());

				HttpEntity resEntity = response.getEntity();
				if (resEntity != null) {
					result = EntityUtils.toString(resEntity, charset);
				}
			}

			httpClient.getConnectionManager().shutdown();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}

	public String doGet(String url, String charset) {
		HttpClient httpClient = null;
		HttpGet httpGet = null;
		String result = null;
		try {
			httpClient = new SSLClient();
			httpGet = new HttpGet(url);
			// 设置参数
			List<NameValuePair> list = new ArrayList<NameValuePair>();

			HttpResponse response = httpClient.execute(httpGet);
			if (response != null) {
				StatusLine statuesLine = response.getStatusLine();
				LogUtils.info(log, statuesLine.toString());

				HttpEntity resEntity = response.getEntity();
				if (resEntity != null) {
					result = EntityUtils.toString(resEntity, charset);
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}

	public String doGet(HttpClient httpClient, String url, String charset) {
		// HttpClient httpClient = null;
		HttpGet httpGet = null;
		String result = null;
		try {
			// httpClient = new SSLClient();
			httpGet = new HttpGet(url);
			// 设置参数

			HttpResponse response = httpClient.execute(httpGet);
			if (response != null) {
				StatusLine statuesLine = response.getStatusLine();
				LogUtils.info(log, statuesLine.toString());

				HttpEntity resEntity = response.getEntity();
				if (resEntity != null) {
					result = EntityUtils.toString(resEntity, charset);
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}

	public byte[] doGetRetunByte(String url, String charset) {
		HttpClient httpClient = null;
		HttpGet httpGet = null;
		byte[] result = null;
		try {
			httpClient = new SSLClient();
			httpGet = new HttpGet(url);
			// 设置参数
			List<NameValuePair> list = new ArrayList<NameValuePair>();

			HttpResponse response = httpClient.execute(httpGet);
			if (response != null) {
				HttpEntity resEntity = response.getEntity();
				if (resEntity != null) {
					result = EntityUtils.toByteArray(resEntity);
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}

}
